﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;

namespace Univar
{
	public class CacheStore : CacheStore<object>
	{
		public CacheStore(string key) : base(key, Scopes.Default) { }
		public CacheStore(string key, Scopes scope) : base(key, scope) { }
	}

	public class CacheStore<T> : StoreBase<T, CacheStore<T>>
	{
		public TimeSpan LifeTime { get; set; }
		public CacheItemRemovedCallback ItemRemovedCallback { get; set; }
		public CacheItemPriority ItemPriority { get; set; }
		public CacheDependency Dependencies { get; set; }

		public CacheStore(string key) : base(key, Scopes.Default) { }
		public CacheStore(string key, Scopes scope) : base(key, scope) { }

		protected override T GetValue(string key)
		{
			object value = Storage.Cache.Get<object>(key, HttpContext);
			if (value == null)
				return DefaultValue;
			else
				return (T)value;
		}

		protected override void SetValue(string key, T value)
		{
			Storage.Cache.Set<T>(key, value, HttpContext, LifeTime, Dependencies, ItemPriority, ItemRemovedCallback);
		}

		protected override object PlainValue 
		{
			get { return Storage.Cache.Get(Key); }
			set { Storage.Cache.Set(Key, value); }
		}
	}

}
